package com.situ.mall.controller.front;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.mall.entity.Cart;
import com.situ.mall.entity.Category;
import com.situ.mall.entity.Product;
import com.situ.mall.service.ICartService;
import com.situ.mall.service.ICategoryService;
import com.situ.mall.service.IProductService;

@Controller
@RequestMapping("/store")
public class FrontStoreController {
	@Autowired
	private IProductService productService;
	@Autowired
	private ICategoryService categoryService;
	@Autowired
	private ICartService cartService;
	
	@RequestMapping("/getStorePage.shtml")
	public String getStorePage(Integer categoryId, Model model,@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "9")Integer limit) {
		System.out.println("FrontProductController.getStorePage()");
		System.out.println("categoryId" + categoryId);
		System.out.println("page" + page);

		int count = categoryService.getTotalCountByCategoryId(categoryId);
		System.out.println("count = " + count);
		
		//查询相关分类商品
		List<Product> list = productService.selectProductByCategoryId(categoryId,page, limit);
		for (Product product : list) {
			System.out.println(product);
		}
		
		//前台分类下拉行
		List<Category> topCategoryList = categoryService.selectTopCategoryList();
		List<Category> secondCategoryList = categoryService.selectSecondCategoryList();
		
		model.addAttribute("page",page);
		model.addAttribute("count", count);
		model.addAttribute("list", list);
		model.addAttribute("topCategoryList", topCategoryList);
		model.addAttribute("secondCategoryList", secondCategoryList);
		return "store";
	}
	
	@RequestMapping("/selectUserCart.shtml")
	@ResponseBody
	public List<Cart> selectUserCart(Integer userId) {
		System.out.println("FrontProductController.selectUserCart()");
		System.out.println("UserId = " + userId);
		//查询相关新品
		List<Cart> listUC = cartService.selectUserCart(userId);
		for (Cart product : listUC) {
			System.out.println(product);
		}
		
		return listUC;
	}
	
	
}
